// pages/drinkApply/drinkApply.js
import http from '../../utils/http.js';
const app =getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addr: "",
    nickname: "",
    mobile: ""
  },
  _data: {
    channelId: "",
    channelName: ""
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    
    this._data.channelId = options.channelId ? options.channelId : ""
    this._data.channelName = options.channelName ? decodeURIComponent(options.channelName) : ""
    this.getInfo()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.globalData.shareTitle,
      path: '/pages/index/index'
    }
  },
  getInfo(){
    http.request("/machineManage/applyInfo")
    .then(res=>{
      let channelId = res.data.channelId
      if(channelId){
        this._data.channelId = channelId
      }
      let channelName = res.data.channelName
      if(channelName){
        this._data.channelName = channelName
      }
      
      let address = res.data.address
      let nickname = res.data.nickname
      let mobile = res.data.mobile
      // this._data.addr = address ? address : ""
      this.setData({
        addr: address ? address : "",
        nickname: nickname ? nickname : "",
        mobile: mobile ? mobile : ""
      })
    })
  },
  nicknameChange(e){
    let nickname = e.detail.value
    this.setData({
      nickname
    })
  },
  mobileChange(e) {
    let mobile = e.detail.value
    this.setData({
      mobile
    })
  },
  addrChange(e) {
    let addr = e.detail.value
    this.setData({
      addr
    })
  },
  submit(){
    let params = {
      nickname: this.data.nickname,
      mobile: this.data.mobile,
      address: this.data.addr,
      channelId: this._data.channelId,
      channelName: this._data.channelName
    }
    http.request("/machineManage/apply", params)
    .then(res=>{
      wx.showToast({
        title: res.message,
        icon: 'none'
      })
      setTimeout(()=>{
        wx.navigateBack({
          delta: 1
        })
      },1000)
    })
    .catch(res=>{

    })    
  },
  preview(){
    wx.previewImage({
      current: 'http://139.196.138.237:8080/static/images/machine_submit_banner.jpg', // 当前显示图片的http链接
      urls: ["http://139.196.138.237:8080/static/images/machine_submit_banner.jpg"] // 需要预览的图片http链接列表
    })
  }
})